package com.gitee.sxran.dynagraphql.demo;

import cn.hutool.core.io.FileUtil;
import cn.hutool.core.thread.ThreadUtil;
import cn.hutool.json.JSONArray;
import cn.hutool.json.JSONObject;
import cn.hutool.json.JSONUtil;
import cn.hutool.system.SystemUtil;
import com.gitee.sxran.dynagraphql.DynGraphqlResolver;
import com.gitee.sxran.dynagraphql.DynGraphqlResolverAsync;
import com.gitee.sxran.dynagraphql.DynGraphqlResolverSync;

import java.util.concurrent.Callable;
import java.util.concurrent.ExecutionException;

/**
 * 动态graphQHL的实现。
 * 其他的可以实现execPath
 */
public  class DynGraphqlResolverASyncDemo extends DynGraphqlResolverAsync {

    /**
     * @param json 数组
     */
    public DynGraphqlResolverASyncDemo(JSONArray json) {
        super(json);
    }
    @Override
    public  Object execPath(String path , JSONObject argJson, JSONObject alisJson, JSONObject subJson){
        // 模拟交易执行时间。
        Integer sleep = argJson.getInt("sleep",1000);
        System.out.println(  path + "=" + System.currentTimeMillis()+"=sleep="+sleep);
        ThreadUtil.sleep(sleep);
        return argJson.getStr("name","lisi");
    }

    public static void main(String[] args) throws Exception {
        String pathTem = SystemUtil.getUserInfo().getCurrentDir() + "/service/%s.json";
        String body = FileUtil.readUtf8String(String.format(pathTem, "hello"));

        DynGraphqlResolverASyncDemo dynGraphqlResolver = new DynGraphqlResolverASyncDemo(JSONUtil.parseArray(body));
        JSONObject resultJson = dynGraphqlResolver.run();
        log.debug(""+resultJson);
    }

}